package com.ds.proxy;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.rmi.runtime.Log;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @program: Spring-Study
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-04-04
 **/
public class LoggerProxyFactory  {

    public static Object getObj(Object obj){
        ProxyLog proxyLog = new ProxyLog();
        proxyLog.setObject(obj);
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),proxyLog);
    }

}
class ProxyLog implements InvocationHandler{
    private static final Logger logger = LoggerFactory.getLogger(LoggerProxyFactory.class);

    private Object object;

    public void setObject(Object object) {
        this.object = object;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        logger.debug("调用了"+method.getName());
        Object invoke = method.invoke(object, args);
        return invoke;
    }
}